import java.util.Arrays;

/**
 * @author wangj881
 * @date 2024-11-25 17:45
 */
public class L541_2 {

    public static void main(String[] args) {
//        String s = "abcdefg";
        String s = "abcd";
        int k = 2;
        String s1 = new Solution().reverseStr(s, k);
        System.out.println(s1);

    }

    static class Solution {
        public String reverseStr(String s, int k) {
            int length = s.length();
            char[] ch = s.toCharArray();
            int i = length / (2 * k);
            for (int j = 0; j < length; j += 2 * k) {
                if (j + k <= length) {
                    reverse(ch, j, j + k - 1);
                    continue;
                }
                reverse(ch, j, length - 1);
            }
            return new String(ch);
        }

        private void reverse(char[] chars, int begin, int end) {
            while (begin < end) {
                char tmp = chars[begin];
                chars[begin] = chars[end];
                chars[end] = tmp;
                begin++;
                end--;
            }
        }
    }
}
